package com.itheima.controller;

import com.itheima.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/parm")
@ResponseBody
public class ParamController{
    //1.请求对象接收参数
    @RequestMapping("/req1")
    public String param1(HttpServletRequest request){
        System.out.println(request.getParameter("username"));
        return "{'req1':'ok'}";
    }
    //2.变量接收
    @RequestMapping("/req2")
    public String param2(String username,int age){
        System.out.println("username="+username);
        System.out.println("age="+age);
        return "{'req2':'ok'}";
    }
    //3.注解起别名
    /*@RequestParam(value="user")
     *value 参数名称
     * required 是否必须输入
     * defaultValue 默认值
     */
    @RequestMapping("/req3")
    public String param3(@RequestParam(value = "user",required = false,defaultValue = "李蛋蛋") String username,int age){
        System.out.println("username="+username);
        System.out.println("age="+age);
        return "{'req3':'ok'}";
    }
    //4.pojo set 方法，自动匹配属性
    @RequestMapping("/req4")
    @ResponseBody
    public String param4(User user){
        System.out.println(user);
        return "{'req4':'ok'}";
    }
    //5.按照集合方式接收 list（单列集合）
    @RequestMapping("/req5")
    public String param5(@RequestParam List<String> list){
        System.out.println(list);
        return "{'req5':'ok'}";
    }
    //6.双列集合
    @RequestMapping("/req6")
    public String param6(@RequestParam Map map){
        System.out.println(map);
        return "{'req6':'ok'}";
    }
    //7.数组
    @RequestMapping("/req7")
    public String param7(String[] strs){
        System.out.println(Arrays.toString(strs));
        return "{'req7':'ok'}";
    }
    //8.日期类型
    @RequestMapping("/req8")
    public String param8(Date date //默认接收的是2001/2/1
        ,@DateTimeFormat(pattern="yyyy-MM-dd")Date date1 //指定格式的字符串转日期
            ,@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")Date date2 //指定格式的字符串转日期
    ){
        System.out.println(date);
        System.out.println(date1);
        System.out.println(date2);
        return "{'req8':'ok'}";
    }
}
